package com.msl.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.msl.MslApp;
import com.msl.R;
import com.msl.adapter.DJTZAdapter;
import com.msl.base.BaseActivity;
import com.msl.bean.DJTZBean;
import com.msl.scan.BroadcastConfig;
import com.msl.tool.ToastUtils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

//条码删除
public class DJTZcodeActivity extends BaseActivity {
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvright)
    TextView tvright;
    @Bind(R.id.list)
    ListView list;
    @Bind(R.id.ivp)
    ImageView ivp;
    @Bind(R.id.login_p)
    EditText loginP;
    @Bind(R.id.tvs)
    TextView tvs;
    private String tag = "DJTZcodeActivity";
    String snid;
    private DJTZAdapter sa;
    private DJTZBean bean;

    protected BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BroadcastConfig.CUSTOM_NAME)) {
                loginP.setText(intent.getStringExtra("scannerdata") );
                waitDialog.show();
                deletecode(MslApp.url+"?action=backcodescan&cid="+MslApp.sSharedPreferences.getString(MslApp.cidk,"")
                        +"&userID="+MslApp.sSharedPreferences.getInt(MslApp.userID,0)
                        +"&storeID="+MslApp.sSharedPreferences.getInt(MslApp.storeID,0)
                        +"&code="+intent.getStringExtra("scannerdata")
                        +"&snid="+snid
                );
            } else if (intent.getAction().equals("netcsucc")) {
                tvs.setText("网络状态:正常");
            } else if (intent.getAction().equals("netcfail")) {
                tvs.setText("网络状态:异常");
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_djtz_code);
        ButterKnife.bind(this);
        tvTitle.setText("条码删除");
        initBroadcastReciever();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConfig.CUSTOM_NAME);
        intentFilter.addAction("netcsucc");
        intentFilter.addAction("netcfail");
        registerReceiver(mBroadcastReceiver, intentFilter);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        snid=simpleDateFormat.format(date);
//        loginP.setText("91578941958015");
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mBroadcastReceiver);
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    void deletecode(String url) {
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    waitDialog.dismiss();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {//回调的方法执行在子线程。
                        result = response.body().string();
                        Log.e(tag, result);
                        waitDialog.dismiss();
                        handler.sendEmptyMessage(0);
                    } else {
                        Log.e(tag, "获取数据失败了" + response.code());
                        waitDialog.dismiss();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            waitDialog.dismiss();
        }
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    bean = gson.fromJson(result, DJTZBean.class);
                    if (bean.getErrcode() == 0) {
                        sa = new DJTZAdapter(DJTZcodeActivity.this, bean.getData());
                        list.setAdapter(sa);
                    } else {
                        ToastUtils.showSingleToast(DJTZcodeActivity.this, bean.getErrmsg());
                    }
                    break;
                default:
                    break;

            }
        }
    };

    @OnClick({R.id.iv_title_left, R.id.ivp})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.ivp:
                if(loginP.getText().toString().length()==0)
                {
                    ToastUtils.showSingleToast(DJTZcodeActivity.this,"数码不能为空");
                    return;
                }
                waitDialog.show();
                deletecode(MslApp.url+"?action=backcodescan&cid="+MslApp.sSharedPreferences.getString(MslApp.cidk,"")
                        +"&userID="+MslApp.sSharedPreferences.getInt(MslApp.userID,0)
                        +"&storeID="+MslApp.sSharedPreferences.getInt(MslApp.storeID,0)
                        +"&code="+loginP.getText().toString()
                        +"&snid="+snid
                );
                break;
        }
    }
}
